package com.baidu.travel.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.sensitivewordfiltering.service.SensitiveWordService;
import com.baidu.travel.domain.*;
import com.baidu.travel.service.CommentService;
import com.baidu.travel.service.WeatherService;
import com.baidu.travel.service.impl.CommentServiceImpl;
import com.baidu.travel.service.impl.WeatherServiceImpl;
import com.baidu.travel.utils.JedisUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pfk
 * @creatTime 2021/07/13下午 04:24
 * @describe
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private CommentService commentService;
    @Autowired
    private WeatherService weatherService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SensitiveWordService sensitiveWordService;





//    /**
//     * 发送评论
//     * @param rid
//     * @param content
//     * @param request
//     * @return
//     * @throws JsonProcessingException
//     */
//    @RequestMapping("/sendComment")
//    @ResponseBody
//    public ResultInfo sendComment(String rid, String content, HttpServletRequest request) throws JsonProcessingException {
//        //获取登录用户
//        User redisObject = (User) JedisUtil.getRedisObject(request.getSession().getId());
//        if(redisObject==null){
//            //用户没有登录
//            ResultInfo r = new ResultInfo();
//            r.setFlag(false);
//            r.setErrorMsg("您还没有登录");
//            return r;
//        }
//
//        //调用service的方法处理评论的逻辑
//        commentService.addCommit(rid,redisObject.getUid(),content);
//        ResultInfo r = new ResultInfo();
//        r.setFlag(true);
//        r.setErrorMsg("评论成功！！");
//        return r;
//    }
//
//    /**
//     * 展示评论到前台
//     * @return
//     */
//    @RequestMapping("/showComment")
//    @ResponseBody
//    public List<CommentVO> showComment(String rid) throws JsonProcessingException {
//        //获取线路id
//        //调用service得到vo对象
//        List<CommentVO> commit = commentService.getCommit(rid);
//        List<CommentVO> commentVOList = JSON.parseArray((String) redisTemplate.opsForValue().get("USER:COMMENT:ROUTE:" + rid), CommentVO.class);
//        System.out.println(commentVOList);
//        return commit;
//    }


    /**
     * 添加评论
     * @param rid
     * @param content
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/sendComment")
    @ResponseBody
    public ResultInfo sendComment(String rid, String content, HttpServletRequest request) throws JsonProcessingException {
        //获取登录用户
        User redisObject = (User) JedisUtil.getRedisObject(request.getSession().getId());
        if(redisObject==null){
            //用户没有登录
            ResultInfo r = new ResultInfo();
            r.setFlag(false);
            r.setErrorMsg("您还没有登录");
            return r;
        }
        //将传入的评论进行脏字进行替换
        String newContent = sensitiveWordService.replaceSensitiveWords(content);
        //调用service的方法处理评论的逻辑
        commentService.addCommit(rid,redisObject.getUid(),newContent);
        ResultInfo r = new ResultInfo();
        r.setFlag(true);
        r.setErrorMsg("评论成功！！");
        return r;
    }

    /**
     * 展示评论到前台
     * @return
     */
    @RequestMapping("/showComment")
    @ResponseBody
    public List<CommentVO> showComment(String rid) throws JsonProcessingException {
        List<CommentVO> commentRedisList = JSON.parseArray((String) redisTemplate.opsForValue().get("USER:COMMENT:ROUTE:" + rid), CommentVO.class);
        //System.out.println(commentRedisList);
        //判断redis中是否有当前线路的评论
        if (commentRedisList==null){
            //没有就去数据库查  如果数据库的也为空就返回一个空
            //不为空就存在redis中
            List<CommentVO> commit = commentService.getCommit(rid);
            if (commit==null||commit.size()==0){
                CommentVO commentVO = new CommentVO();
                commentVO.setText("这儿超棒!");
                commentRedisList=new ArrayList<>();
                commentRedisList.add(commentVO);
                return commentRedisList;
            }
            redisTemplate.opsForValue().set("USER:COMMENT:ROUTE:"+rid,commit);
            return commit;
        }
        return commentRedisList;
    }




    /**
     * 获取天气情况
     * @param city
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/getWeather")
    @ResponseBody
    public String getWeather(String city) throws JsonProcessingException {
        //获取需要查询的城市
        //调用service层获得天气对象
        Weather weather = weatherService.getWeatherByCite(city);
        String string = objectMapper.writeValueAsString(weather);
        return string;
    }

    /**
     * 当前用户的最新评论显示
     * @return
     */
    @GetMapping("/latestcomments")
    @ResponseBody
    public Map<String,String> latestComments(String rid,HttpServletRequest request){
        User redisObject = (User) JedisUtil.getRedisObject(request.getSession().getId());
        //User redisObject=(User) redisTemplate.opsForValue().get(request.getSession().getId());
        String comment=commentService.latestComments(rid,redisObject.getUid());
        Map<String, String> map = new HashMap<>();
        map.put("str", comment);
        return map;
    }
}
